
package com.scalar.chiptrack.events;

// Balaram, Jun 23 2005
public class SSEventManager
{

    private static SSEventManager sEventManager = new SSEventManager();
    private SSEventListenerMap mListnerMap = null;


    private SSEventManager()
    {
        mListnerMap = SSEventListenerDefinitionParser.loadEventListenersFromConfigFile();
    }

    public static SSEventManager getInstance()
    {
        return sEventManager;
    }

    public SSEventListenerMap getAllAddedListener()
    {
        return mListnerMap;
    }

    public void addEventListener( String eventType, SSEventListener listener )
    {
        SSEventListenerList list = mListnerMap.get1( eventType );

        if( list == null )
        {
            list = new SSEventListenerList();
        }

        list.add( listener );

        mListnerMap.put( eventType, list );
    }

    public void removeEventListener( String eventType, SSEventListener listener )
    {
        SSEventListenerList list = mListnerMap.get1( eventType );

        if( ( list != null ) && ( list.isEmpty() != true ) )
        {
            list.remove( listener );
        }
    }

    public void dispatchEvent( SSEvent event ) throws Exception
    {
        if( mListnerMap.get( event.getName() ) == null )
        {
            System.out.println( "No Listeners available for the event : " + event.getType() );
        }
        else
        {
            SSEventListenerIterator iterator = mListnerMap.get1( event.getName().getValue() ).iterator1();
            SSEventListener listener = null;

            while( iterator.hasNext() == true )
            {
                listener = iterator.next();

                try
                {
                    listener.handleEvent( event );
                }
                catch( Throwable t )
                {
                    System.out.println( "Error while dispatching the SSEvent to the registered listeners" );
                    t.printStackTrace();
                }
            }
        }
    }


    public void dispatchEvent( SSEventList eventList ) throws Exception
    {
        if( mListnerMap.get( eventList.getEventType() ) == null )
        {
            System.out.println( "No Listeners available for the event : " + eventList.getEventType() );
        }
        else
        {
            SSEventListenerIterator iterator = mListnerMap.get1( eventList.getEventType() ).iterator1();
            SSEventListener listener = null;

            while( iterator.hasNext() == true )
            {
                listener = iterator.next();

                try
                {
                    System.out.println( "Firing event: " + eventList.getEventType() + ", on: " + listener );
                    listener.handleEvent( eventList );
                }
                catch( Throwable t )
                {
                    System.out.println( "Error while dispatching the SSEvent to the registered listeners" );
                    t.printStackTrace();
                }
            }
        }
    }

    public static void main( String[] args ) throws Exception
    {
        /*SSEventManager.getInstance().dispatchEvent(
            new DocumentChangeEvent( null,
                                     DocumentChangeEventType.ALL_DOCUMENT_CHANGES,
                                     "System",
                                     null,
                                     true ) );
        */
    }

}

